﻿using System;
using System.Linq;
using System.Web.Mvc;
using Framework.Domain;
using Shell.DomainDot;
using Framework.Poco;

namespace Framework.Mvc.Controllers
{
    /// <summary>
    /// 经销商控制器
    /// </summary>
    public class AgencyManagerController : Controller
    {
        private IDomainDotFactory factory;

        /// <summary>
        /// 构造器
        /// </summary>
        public AgencyManagerController()
        {
            factory = DomainDotFactory.Factory;
        }

        /// <summary>
        /// 经销商列表
        /// </summary>
        public ActionResult List(User currentUser, int currentPage, int pageSize)
        {
            if (currentUser != null && currentUser.IsAdmin)
            {
                var context = factory.Create<AgencyManagerContext>();
                var agencies = context.GetAllAgencies().OrderByDescending(m => m.Created).Skip((currentPage - 1) * pageSize).Take(pageSize);
                AgencyPagedViewModel model = new AgencyPagedViewModel(agencies, currentPage, pageSize, context.Agencies.Count);
                return View(model);
            }
            return this.RedirectToAction("Index", "Home");
        }

        /// <summary>
        /// 经销商详情
        /// </summary>
        public ActionResult Detail(Guid agencyIdentity)
        {
            var context = factory.Create<AgencyManagerContext>();
            var agency = context.Agencies.SingleOrDefault(a => a.Id == agencyIdentity);
            if (agency != null)
            {
                AgencyDetailViewModel model = new AgencyDetailViewModel(agency);
                return View(model);
            }
            this.ModelState.AddModelError("Agency", "Could not be found！");
            return this.RedirectToAction("List");
        }

        /// <summary>
        /// 经销商删除
        /// </summary>
        [HttpGet]
        public ActionResult Delete(User currentUser, AgencyDeleteViewModel model)
        {
            if (currentUser != null && currentUser.IsAdmin)
            {
                return View(model);
            }
            return this.RedirectToAction("Index", "Home");
        }

        /// <summary>
        /// 经销商删除
        /// </summary>
        [HttpPost]
        public ActionResult Delete(User currentUser, Guid identity)
        {
            if (currentUser != null && currentUser.IsAdmin)
            {
                var agencyManagerContext = factory.Create<AgencyManagerContext>();
                var original = agencyManagerContext.GetAllAgencies().SingleOrDefault(a => a.Id == identity);
                if (original != null)
                {
                    var agencyContext = factory.Create<AgencyContext>(original.Id);
                    agencyContext.ReleaseAgencyGroup();
                    agencyContext.ReleaseDistrict();
                    agencyContext.DeleteContacts();
                    agencyManagerContext.Remove(identity);
                }
                return this.RedirectToAction("List");
            }
            return this.RedirectToAction("Index", "Home");
        }

        /// <summary>
        /// 经销商修改
        /// </summary>
        public ActionResult Modify(Guid agencyIdentity, User currentUser)
        {
            if (currentUser != null && currentUser.IsAdmin)
            {
                var context = factory.Create<AgencyManagerContext>();
                var agency = context.Agencies.SingleOrDefault(a => a.Id == agencyIdentity);
                if (agency != null)
                {
                    Session["agencyName"] = agency.Name;
                    return this.RedirectToAction("Index", "Agency");
                }
                else
                {
                    this.ModelState.AddModelError("Agency", "Could not be found！");
                }
                return this.RedirectToAction("List");
            }
            return this.RedirectToAction("Index", "Home");
        }

        /// <summary>
        /// 经销商是否通过验证
        /// </summary>
        public ActionResult Verification(User currentUser, Guid identity, bool isVerification)
        {
            if (currentUser != null && currentUser.IsAdmin)
            {
                var context = factory.Create<AgencyManagerContext>();
                var agency = context.Agencies.SingleOrDefault(a => a.Id == identity);
                if (agency != null)
                {
                    context.SetVerification(agency, isVerification);
                    return this.RedirectToAction("List");
                }
                else
                {
                    this.ModelState.AddModelError("Agency", "Could not be found！");
                }
            }
            return this.RedirectToAction("Index", "Home");
        }

        /// <summary>
        /// 经销商设置等级
        /// </summary>
        [HttpPost]
        public ActionResult SetLevel(Guid agencyIdentity, int level)
        {
            var context = factory.Create<AgencyManagerContext>();
            var agency = context.Agencies.SingleOrDefault(a => a.Id == agencyIdentity);
            if (agency != null)
            {
                context.SetLevel(agency, level);
                return this.RedirectToAction("List");
            }
            else
            {
                this.ModelState.AddModelError("Agency", "Could not be found！");
            }
            return this.RedirectToAction("Detail");
        }
    }
}